﻿#region Copyright information
// 
// Copyright © 2005-2013 Yongkee Cho. All rights reserved.
// 
// This code is a part of the Biological Object Library and governed under the terms of the
// GNU Lesser General  Public License (LGPL) version 2.1 which accompanies this distribution.
// For more information on the LGPL, please visit http://bol.codeplex.com/license.
// 
// - Filename: IStateMachine.cs
// - Author: Yongkee Cho
// - Email: yongkeecho@gmail.com
// - Date Created: 2013-01-24 4:34 PM
// - Last Modified: 2013-01-25 3:59 PM
// 
#endregion
using System;
using System.Collections.Generic;
using BOL.Maths.Distributions;

namespace BOL.Algorithms.StateMachines
{
    public interface IStateMachine<TState, TTransition> : IEquatable<IStateMachine<TState, TTransition>>
    {
        IDictionary<TState, double> InitialDistribution { get; set; }
        IDictionary<TState, IDictionary<TState, TTransition>> TransitionDistribution { get; set; }
        
        IEnumerable<TState> Generate(Random r, int length);
    }

    public interface IMarkovChain<TState> : IEquatable<IMarkovChain<TState>>
    {
        IDictionary<TState, double> InitialDistribution { get; set; }
        IDictionary<TState, IDistribution<TState>> TransitionDistribution { get; set; }
        
        IEnumerable<TState> Generate(Random r, int length);
        void Train(IEnumerable<TState> source);
        void Train(IEnumerable<IEnumerable<TState>> source);
    }

    public interface IHiddenMarkovModel<TState, TObservable> : IMarkovChain<TState>, IEquatable<IHiddenMarkovModel<TState, TObservable>>
        where TObservable : struct, IComparable
    {
        IDictionary<TState, IDistribution<TObservable>> EmissionDistribution { get; set; }
        
        new IEnumerable<Tuple<TState, TObservable>> Generate(Random r, int length);
        void Train<TSource>(IEnumerable<IEnumerable<TSource>> sequences, Func<TSource, TState> stateSelector, Func<TSource, TObservable> observableSelector);
        void ViterbiTrain(IEnumerable<IEnumerable<TObservable>> observables, int numberOfIterations);
        void BaumWelch(IEnumerable<TObservable> observables, int numberOfIterations);
        IEnumerable<TState> Viterbi(IEnumerable<TObservable> observables);
        IDictionary<TState, double[]> DecodePosterior(IEnumerable<TObservable> observables);
    }

    public interface IGeneralizedHiddenMarkovModel<TState, TObservable> : IHiddenMarkovModel<TState, TObservable>, IEquatable<IGeneralizedHiddenMarkovModel<TState, TObservable>>
        where TObservable : struct, IComparable
    {
        IDictionary<TState, IDistribution<int>> LengthDistribution { get; set; }
        new IEnumerable<Tuple<TState, TObservable>> Generate(Random r, int length);
        new void Train<TSource>(IEnumerable<IEnumerable<TSource>> sequences, Func<TSource, TState> stateSelector, Func<TSource, TObservable> observableSelector);
        new void ViterbiTrain(IEnumerable<IEnumerable<TObservable>> observables, int numberOfIterations);
        new void BaumWelch(IEnumerable<TObservable> observables, int numberOfIterations);
        new IEnumerable<TState> Viterbi(IEnumerable<TObservable> observables);
        new IDictionary<TState, double[]> DecodePosterior(IEnumerable<TObservable> observables);
    }

    //public interface IMaximumEntropyMarkovModel<TState, TObservable> : IHiddenMarkovModel<TState, TObservable>, IEquatable<IGeneralizedHiddenMarkovModel<TState, TObservable>>
    //    where TObservable : struct, IComparable
    //{
    //    new IDictionary<TState, IDictionary<TState, Func<TState, TObservable, double>>> TransitionDistribution { get; set; }
    //    new IEnumerable<Tuple<TState, TObservable>> Generate(Random r, int length);
    //}

    //public interface IConditionalRandomField<TState, TObservable> : IHiddenMarkovModel<TState, TObservable>, IEquatable<IGeneralizedHiddenMarkovModel<TState, TObservable>>
    //where TObservable : struct, IComparable
    //{
    //    new IDictionary<TState, IDictionary<TState, Func<TState, TObservable, double>>> TransitionDistribution { get; set; }
    //    new IEnumerable<Tuple<TState, TObservable>> Generate(Random r, int length);
    //}
}
